package com.morse.login;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import com.morse.apps.App_tables;
import com.morse.author.AboutDialog;
import com.morse.mzjw.R;
import com.morse.utils.*;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

/**
 * @author morse
 * @创建时间2014-6-20
 */

public class Login extends Activity {
	/** Called when the activity is first created. */

	private EditText user;
	private EditText password;
	private Button loginBtn;
	private ProgressDialog m_Dialog = null;
	private CheckBox cbrp;
	private SharedPreferences sp;

	private Handler handler = new Handler();
	DefaultHttpClient mHttpClient = new DefaultHttpClient();

	private static Boolean isExit = false;
	private static Boolean hasTask = false;
	Timer tExit = new Timer();

	private String username = "";
	private String pass = "";
	
	private static String myUrl = "jw6.csmzxy.com:8088";//默认网段服务器
	//private static String myUrl = "host.csmzxy.com/zfca/login";//默认网段服务器

	private String xh = "";
	private String xm = "";

	private int result = 0;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		user = (EditText) findViewById(R.id.user);
		password = (EditText) findViewById(R.id.password);
		//signcode = (EditText) findViewById(R.id.yzcode);

		loginBtn = (Button) findViewById(R.id.loginBtn);
		loginBtn.setOnClickListener(loginClick);

		cbrp = (CheckBox) findViewById(R.id.cbrp);				

		// 请求超时
		mHttpClient.getParams().setParameter(
				CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
		// 读取超时
		mHttpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
				10000);

		InitConfig();
		MyApp myApp = (MyApp) getApplication();
		//获取地址赋给myUrl
		//myUrl = SelectServer.getAddress();
		myApp.setMyUrl(myUrl);
		cbrp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				sp = getSharedPreferences("UserInfo", 0);
				sp.edit().putBoolean("cbrp", isChecked).commit();
			}
		});

	}	
	
	private boolean validate(){
		String username = user.getText().toString();
		if(username.equals("")){
			showDialog("请输入学号");
			return false;
		}
		String pwd = password.getText().toString();
		if(pwd.equals("")){
			showDialog("请输入密码");
			return false;
		}
		return true;
	}
	private void showDialog(String msg){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(msg)
		       .setCancelable(false)
		       .setPositiveButton("确定", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		           }
		       });
		AlertDialog alert = builder.create();
		alert.show();
	}
	
	TimerTask task = new TimerTask() {
		public void run() {
			isExit = false;
			hasTask = true;
		}
	};
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_DOWN) {
			// DialogAPI.showExit(this);
			if (isExit == false) {
				isExit = true;
				Toast.makeText(getApplicationContext(), "再按一次退出程序",
						Toast.LENGTH_SHORT).show();
				if (!hasTask) {
					tExit.schedule(task, 2000);
				}
			} else {
				finish();
				System.exit(0);
			}
			return false;
		}

		return super.onKeyDown(keyCode, event);
	}
	//菜单
	public boolean onCreateOptionsMenu(Menu menu) {		
		menu.add(Menu.NONE, Menu.FIRST + 1, 1, "网络设置").setIcon(
				android.R.drawable.ic_menu_manage);
		menu.add(Menu.NONE, Menu.FIRST + 2, 2, "关于软件").setIcon(
				android.R.drawable.ic_menu_info_details);
		//menu.add(Menu.NONE, Menu.FIRST + 3, 3, "软件更新").setIcon(
		//		android.R.drawable.ic_menu_more);

		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case Menu.FIRST + 1:
			Intent intent = new Intent(Login.this, SetNet.class);
			Login.this.startActivity(intent);
			break;

		case Menu.FIRST + 2:
			//Toast.makeText(this, "Code by morse \n GUI by unique", Toast.LENGTH_SHORT).show();
			new AboutDialog(this).show();
			break;
		/*	
		case Menu.FIRST + 3:
			Toast.makeText(this, "最后更新:2014-6-12 20:01:30", Toast.LENGTH_SHORT).show();
			Intent intent2 = new Intent(Login.this, UpdateActivity.class);
			Login.this.startActivity(intent2);
			break;
		*/
		}		
		return false;
	}

	// 初始化配置
	private void InitConfig() {
		sp = getSharedPreferences("UserInfo", 0);
		user.setText(sp.getString("account", null));
		password.setText(sp.getString("password", null));
		cbrp.setChecked(sp.getBoolean("cbrp", false));
	}	
						 				
	OnClickListener loginClick = new OnClickListener() {
		
		public void onClick(final View v) {
			
			if(validate()){
				//用户名和密码都填了执行				
				m_Dialog = ProgressDialog.show(Login.this, "请稍后", "登录中..",true);
				new Thread(new Runnable() {
					public void run() {
						MyApp myApp = (MyApp) getApplication();
						if (myApp.getMyUrl() != null && myApp.getMyUrl() != "") {
							myUrl = myApp.getMyUrl();
						}
						result = checkUser();// 获取登陆情况
						// 更新界面
						handler.post(new Runnable() {
							@SuppressLint({ "WorldReadableFiles", "WorldWriteableFiles" })
							@SuppressWarnings("deprecation")
							public void run() {
								if (result == 1) {
									if (cbrp.isChecked()) {
										sp = getSharedPreferences(
												"UserInfo",
												Context.MODE_WORLD_WRITEABLE
														| Context.MODE_WORLD_READABLE);
	
										sp.edit().putString("account",user.getText().toString()).commit();
										sp.edit().putString("password",password.getText().toString()).commit();
									}
									Toast.makeText(v.getContext(),"登录成功!欢迎" + xm +"同学", 
											Toast.LENGTH_LONG).show();
									Context context = v.getContext();
									Intent intent = new Intent(context,App_tables.class);
									Bundle map = new Bundle();
									map.putSerializable("xh", xh);
									map.putSerializable("xm", xm);
									intent.putExtra("session", map);
									context.startActivity(intent);
									Login.this.finish();
								}
								if (result == 0) {
									Toast.makeText(v.getContext(), "用户验证失败",
											Toast.LENGTH_SHORT).show();
								}
								if (result == -1) {
									Toast.makeText(v.getContext(), "请检查网络连接！",
											Toast.LENGTH_SHORT).show();
								}
								if (result == -2) {
									Toast.makeText(v.getContext(), "错误原因：系统正忙！",
											Toast.LENGTH_SHORT).show();
								}
	
							}
					});	
					m_Dialog.dismiss();
				}
			}).start();
		
		}else{
			//showDialog("用户名称或者密码错误，请重新输入！");
		}
	}
};

	private int checkUser() {

		// 此处先获取页面，读取到value值为post做准备
		String __VIEWSTATE = "";
		String temp = "";
		StringTokenizer tokenizer = null;

		try {
			temp = HttpUtil.getUrl("http://" + myUrl + "/default2.aspx",
					mHttpClient, "");
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		if (temp == "") {
			return -1;
		}
		tokenizer = new StringTokenizer(temp);
		while (tokenizer.hasMoreTokens()) {
			String valueToken = tokenizer.nextToken();
			if (StringUtil.isValue(valueToken, "value")) {
				if (StringUtil.getValue(valueToken, "value", "\"", 7).length() == 48) {
					__VIEWSTATE = StringUtil.getValue(valueToken, "value", "\"", 7);// value
				}
			}
		}

		username = user.getText().toString();
		pass = password.getText().toString();

		List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("__VIEWSTATE", __VIEWSTATE));
		pairs.add(new BasicNameValuePair("Button1", null));
		pairs.add(new BasicNameValuePair("RadioButtonList1", "%D1%A7%C9%FA"));	
		pairs.add(new BasicNameValuePair("TextBox1", username));
		pairs.add(new BasicNameValuePair("TextBox2", pass));
		//pairs.add(new BasicNameValuePair("TextBox3", yzcode));	
		pairs.add(new BasicNameValuePair("lbLanguage", null));

		String info = "";
		try {
			info = HttpUtil.postUrl("http://" + myUrl + "/default2.aspx",
					pairs, mHttpClient, "");
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (info != "") {
			MyApp myApp = (MyApp) getApplication();
			myApp.setmHttpClient(mHttpClient);// 将对象放入app
			tokenizer = new StringTokenizer(info);

			while (tokenizer.hasMoreTokens()) {
				String valueToken = tokenizer.nextToken();
				if (StringUtil.isValue(valueToken, "defer")) {
					return 0;
				}
				if (StringUtil.isValue(valueToken, " <title>ERROR")) {
					return -2;
				}
				if (StringUtil.isValue(valueToken, "id=\"xhxm")) {
					xh = StringUtil.getValue(valueToken, "id=\"xhxm", "<", 10);// value
					xm = StringUtil.getValue(tokenizer.nextToken().toString(), "",
							"同学", 0);// 从登陆后页面取得姓名
				}
			}
			return 1;
		} else {
			return -1;
		}		
	}	
}